package com.cxd.app.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cxd.app.service.MyAccountService;
import com.cxd.app.service.UserService;
import com.cxd.base.common.exception.CxdException;
import com.cxd.base.controller.BaseController;
import com.cxd.base.util.BeanTools;
import com.cxd.base.util.date.DateStyle;

/**
 * 我的账户
 * @author wtj
 * @date 2015年12月5日 下午3:09:47
 * @company CXD
 * @version [Copyright (c) 2015 V001]
 */
@Controller("MyAccountController")
@RequestMapping("myaccountc")
public class MyAccountController extends BaseController{
	@Resource
	public BeanTools bt;
	
	@Resource
	public MyAccountService myaccountService;
	
	@Resource
	public UserService userService;
	
	private static final Logger logger = Logger.getLogger(OtherController.class);
	
	/**
	 * 账户余额查询
	 * @author wtj
	 * @date 2015年12月5日下午3:12:28
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "accountBalance", method = {RequestMethod.GET,RequestMethod.POST })
	public void accountBalance(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		JSONObject json = new JSONObject();
		try {
			myaccountService.accountBalance(json,value);
			this.jsonMsg(json, 200, "成功");
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, json);
	}
	
	/**
	 * 绑卡查询
	 * @author wtj
	 * @date 2015年12月10日上午9:33:48
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "bindCardQuery", method = {RequestMethod.GET,RequestMethod.POST })
	public void bindCardQuery(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		JSONObject json = new JSONObject();
		try {
			json = myaccountService.bindCardQuery(json,value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, json);
	}
	
	/**
	 * 解绑 解除银行卡绑定
	 * @author wtj
	 * @date 2016年1月10日下午5:27:30
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "unCardBind", method = {RequestMethod.GET,RequestMethod.POST })
	public void unCardBind(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		super.requestLog("unCardBind",value);
		JSONObject json = new JSONObject();
		try {
			if(!userService.checkToken(request)){
				this.jsonMsg(json, 403, "用户未登录");
			}else{
				json = myaccountService.unCardBind(json,value);
				if(json.get("status")==null){
					this.jsonMsg(json, 200, "成功");
				}
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, json);
	}
	/**
	 * 用户还款收益信息
	 * @author wtj
	 * @date 2015年12月10日上午11:08:22
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "repayProfit", method = {RequestMethod.GET,RequestMethod.POST })
	public void repayProfit(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		JSONObject json = new JSONObject();
		try {
			json = myaccountService.repayProfit(json,value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, json);
	}
	
	/**
	 * 用户投资消息
	 * @author wtj
	 * @date 2015年12月10日上午11:08:22
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "investProfit", method = {RequestMethod.GET,RequestMethod.POST })
	public void investProfit(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		JSONObject json = new JSONObject();
		try {
			json = myaccountService.investProfit(json,value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, json);
	}
	
	/**
	 * 用户交易记录
	 * @author wtj
	 * @date 2015年12月10日下午3:00:43
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "transactionRecords", method = {RequestMethod.GET,RequestMethod.POST })
	public void transactionRecords(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		JSONObject json = new JSONObject();
		try {
			json = myaccountService.transactionRecords(json,value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, JSON.toJSONStringWithDateFormat(json, DateStyle.YYYY_MM_DD_HH_MM_SS.getValue(), SerializerFeature.WriteDateUseDateFormat));
	}
	
	/**
	 * 充值
	 * @author wtj
	 * @date 2015年12月11日上午10:24:40
	 * @param json
	 * @param value
	 * @throws IOException 
	 */
	@RequestMapping(value = "recharge", method = {RequestMethod.GET,RequestMethod.POST })
	public void recharge(HttpServletRequest request,HttpServletResponse response,String value) throws IOException{
		super.requestLog("recharge",value);
		Long beginTime = System.currentTimeMillis();
		JSONObject json = new JSONObject();
		try {
			json = myaccountService.recharge(json,value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(beginTime,response, json.toString());
	}
	
	/**
	 * 用户提现
	 * @author wtj
	 * @date 2015年12月11日下午3:59:05
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "withdraw", method = {RequestMethod.GET,RequestMethod.POST })
	public void withdraw(HttpServletRequest request,HttpServletResponse response,String value) throws IOException{
		super.requestLog("withdraw",value);
		JSONObject json = new JSONObject();
		try {
			json = myaccountService.withdraw(json,value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, json);
	}
	
	/**
	 * 绑定银行卡 绑卡
	 * @author wtj
	 * @date 2015年12月14日上午10:26:50
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "bindBankCard", method = {RequestMethod.GET,RequestMethod.POST })
	public void bindBankCard(HttpServletRequest request,HttpServletResponse response,String value) throws IOException{
		super.requestLog("bindBankCard",value);
		JSONObject json = new JSONObject();
		try {
			json = myaccountService.bindBankCard(json,value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "银行卡绑定成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, json);
	}
}
